package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.api.dto.UserDto;
import com.coin.domain.WorkIssue;
import com.coin.feignclient.SysUserFeignClient;
import com.coin.mapper.WorkIssueMapper;
import com.coin.service.WorkIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 李力
 * @description 针对表【work_issue(工单记录)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue>
        implements WorkIssueService {

    @Autowired
    private SysUserFeignClient sysUserFeignClient;

    /**
     * @Description: 分页条件查询工单
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: Page<WorkIssue> page, Integer status, String startTime, String endTime
     * @return: R
     */
    @Override
    public Page<WorkIssue> findByPage(Page<WorkIssue> page, Integer status, String startTime, String endTime) {
        LambdaQueryWrapper<WorkIssue> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(status != null, WorkIssue::getStatus, status)
                .between(StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime), WorkIssue::getCreated, startTime, endTime);
        Page<WorkIssue> paged = page(page, wrapper);
        List<WorkIssue> records = paged.getRecords();
        if (records == null || records.isEmpty()) {
            return paged;
        }
        //远程调用member-service
        List<Long> userIds = records.stream().map(WorkIssue::getUserId).collect(Collectors.toList());
        List<UserDto> userDtos = sysUserFeignClient.getBasicUsers(userIds);
        if (userDtos == null || userDtos.isEmpty()) {
            return paged;
        }
        Map<Long, UserDto> map = userDtos.stream().collect(Collectors.toMap(UserDto::getId, userDto -> userDto));
        records.forEach(record -> {
            record.setUsername(map.get(record.getUserId()).getUsername());
            record.setRealName(map.get(record.getUserId()).getRealName());
        });
        paged.setRecords(records);
        return paged;
    }

    /**
     * @Description: 回复某个工单
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: id answer
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean answer(Long id, String answer) {
        if (id == null || StrUtil.isEmpty(answer)) {
            throw new RuntimeException("参数为空");
        }
        WorkIssue workIssue = new WorkIssue();
        workIssue.setId(id);
        workIssue.setAnswer(answer);
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        workIssue.setAnswerUserId(Long.parseLong(userId));
        return updateById(workIssue);
    }

    @Override
    public Page<WorkIssue> viewWorkIssue(Page<WorkIssue> page, Long userId) {
        LambdaQueryWrapper<WorkIssue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(WorkIssue::getUserId, userId);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 会员添加工单
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @Override
    public boolean addWorkIssue(WorkIssue workIssue) {
        return save(workIssue);
    }
}




